//-----------------------------------------------------------------------
// <copyright file="a_ShipCostDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_ShipCostDAOBase
    {
        #region Common methods
        public virtual a_ShipCost Createa_ShipCostFromReader(IDataReader reader)
        {
            a_ShipCost item = new a_ShipCost();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ShipCostID"))) item.ShipCostID = (int)reader["ShipCostID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ShipServiceID"))) item.ShipServiceID = (int)reader["ShipServiceID"];
                if (!reader.IsDBNull(reader.GetOrdinal("WeightMin"))) item.WeightMin = (decimal)reader["WeightMin"];
                if (!reader.IsDBNull(reader.GetOrdinal("WeightMax"))) item.WeightMax = (decimal)reader["WeightMax"];
                if (!reader.IsDBNull(reader.GetOrdinal("ShipCost"))) item.ShipCost = (decimal)reader["ShipCost"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Type"))) item.Type = (byte)reader["Type"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_ShipCost methods
            
        public virtual void Createa_ShipCost(a_ShipCost a_ShipCost)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipCosts_ins");
                
                database.AddInParameter(dbCommand, "@ShipServiceID", DbType.Int32, a_ShipCost.ShipServiceID);
                database.AddInParameter(dbCommand, "@WeightMin", DbType.Decimal, a_ShipCost.WeightMin);
                database.AddInParameter(dbCommand, "@WeightMax", DbType.Decimal, a_ShipCost.WeightMax);
                database.AddInParameter(dbCommand, "@ShipCost", DbType.Currency, a_ShipCost.ShipCost);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_ShipCost.DomainID);
                database.AddInParameter(dbCommand, "@Type", DbType.Byte, a_ShipCost.Type);
                database.AddOutParameter(dbCommand, "@ShipCostID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_ShipCost.ShipCostID = (int)database.GetParameterValue(dbCommand, "@ShipCostID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_ShipCost methods
        
        public virtual void Updatea_ShipCost(a_ShipCost a_ShipCost)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipCosts_upd");            
                
                database.AddInParameter(dbCommand, "@ShipCostID", DbType.Int32, a_ShipCost.ShipCostID);
                database.AddInParameter(dbCommand, "@ShipServiceID", DbType.Int32, a_ShipCost.ShipServiceID);
                database.AddInParameter(dbCommand, "@WeightMin", DbType.Decimal, a_ShipCost.WeightMin);
                database.AddInParameter(dbCommand, "@WeightMax", DbType.Decimal, a_ShipCost.WeightMax);
                database.AddInParameter(dbCommand, "@ShipCost", DbType.Currency, a_ShipCost.ShipCost);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_ShipCost.DomainID);
                database.AddInParameter(dbCommand, "@Type", DbType.Byte, a_ShipCost.Type);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_ShipCost methods
        public virtual void Deletea_ShipCost(int shipCostID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipCosts_del");
                
                database.AddInParameter(dbCommand, "@ShipCostID", DbType.Int32, shipCostID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_ShipCost methods
        
        public virtual a_ShipCost Geta_ShipCost(int shipCostID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipCosts_sel");
                
                database.AddInParameter(dbCommand, "@ShipCostID", DbType.Int32, shipCostID);
                
                a_ShipCost a_ShipCost =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_ShipCost = Createa_ShipCostFromReader(reader);
                        reader.Close();
                    }
                }
                return a_ShipCost;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_ShipCostList methods
        public virtual a_ShipCostCollection Geta_ShipCostList(a_ShipCostColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipCosts_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_ShipCostCollection a_ShipCostCollection = new a_ShipCostCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_ShipCost a_ShipCost = Createa_ShipCostFromReader(reader);
                        a_ShipCostCollection.Add(a_ShipCost);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ShipCostCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_ShipCostCollection Geta_ShipCostList(a_ShipCostColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_ShipCostList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}